// pages/orderfood/index.js
import ddt from '../../api/axios.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    money: 0,
    sellmoney: '',
    table: [],
    active: 0,
    commitlist: [],
    list: [],
    adddis: false,
    tid: '',
    fromlist: [],
  },
  changenum(e) {
    if (wx.getStorageSync('userinfo') == '') {
      var that = this
      wx.showModal({
        title: '提示',
        content: '是否前往登陆',
        success(res) {
          if (res.confirm) {
            that.setData({
              table: [],
              list: []
            })
            wx.navigateTo({
              url: '../login/login',
            })
          }
        }
      })
      return
    }
    var num = e.detail
    var oid = e.currentTarget.dataset.id
    var sellnum = e.currentTarget.dataset.stocknum
    if (num >= sellnum + 1) {
      wx.showToast({
        title: '已达到库存上限',
        icon: 'error'
      })
      this.setData({
        adddis: true
      })
      return
    } else {
      this.setData({
        adddis: false
      })
    }
    var from = this.data.fromlist
    var fromcc = from[this.data.active].findIndex((from) => from.id == oid)
    this.data.fromlist[this.data.active][fromcc].amount = num
    var onelist = {
      buy_num: num,
      good_id: oid,
    };
    var goodslist = this.data.commitlist;
    var cl = goodslist.findIndex((goodslist) => goodslist.good_id == oid)
    if (cl == "-1") {
      this.data.commitlist.push(onelist);
    } else {
      this.data.commitlist[cl].buy_num = num;
    }
    this.jisuan()
  },
  jisuan() {
    ddt('post', '/api/calculatemoney', {
      detail: this.data.commitlist
    }).then(res => {
      this.setData({
        money: res.data.data.totalfee
      })
    })
  },
  onchange(e) {
    this.setData({
      active: e.currentTarget.dataset.index,
      tid: e.currentTarget.dataset.tid,
      list: []
    })
    var tid = e.currentTarget.dataset.tid
    if (this.data.fromlist.length < e.currentTarget.dataset.index + 1) {
      this.listinfo(tid)
    } else {
      this.setData({
        list: this.data.fromlist[e.currentTarget.dataset.index]
      })
    }

  },
  gopath1(e) {
    if (e.mark.stepperMark == undefined) {
      wx.navigateTo({
        url: `../goodsdetail/index?name=${e.currentTarget.dataset.name}&id=${e.currentTarget.dataset.id}&active=${this.data.active}`,
      })
    }
  },
  gopath(e) {
    if (wx.getStorageSync('userinfo') == '') {
      wx.showModal({
        title: '提示',
        content: '是否前往登陆',
        success(res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '../login/login',
            })
          }
        }
      })
    } else {
      var aa = this.data.commitlist.filter(v => {
        return v.buy_num != 0
      })
      if (aa.length == 0) {
        wx.showToast({
          title: '请选择一个商品',
          icon: 'error'
        })
      } else if (this.data.money * 1 < this.data.sellmoney) {
        wx.showToast({
          title: '暂未达到起送金额',
          icon: 'error'
        })
      } else {
        this.commt()
      }
    }


  },
  commt() {
    ddt('post', '/api/cart/placeAnOrder', {
      shopid: this.data.shopid,
      detail: this.data.commitlist
    }).then(res => {
      if (res.data.code == 1) {
        wx.navigateTo({
          url: `../submitorder/index?orderid=${res.data.data.order_id} `
        })
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'error'
        })
      }
    })
  },
  listinfo(tid) {
    ddt('get', '/api/products/productslist', {
      "shopid": this.data.shopid, //商店id
      "type": tid //商品分类id
    }).then(res => {
      
      var aa = res.data.data.map(v => {
        return {
          img: v.menuimage.split(',')[0],
          ...v,
          amount: 0
        }
      })
      if (this.data.fromlist.length == 0 || this.data.fromlist[0][0].id != aa[0].id) {
        this.data.fromlist.push(aa)
      }
      this.setData({
        list: aa
      })
    })
  },
  typelist() {
    ddt('post', '/api/products/typelist', {
      shopid: this.data.shopid
    }).then(res => {
      if (res.data.code == 1) {
        this.setData({
          table: res.data.data
        })
        if (this.data.tid == '') {
          this.listinfo(res.data.data[0].id)
        } else {
          if (this.data.fromlist.length >= this.data.active + 1) {
            this.setData({
              list: this.data.fromlist[this.data.active]
            })
          } else {
            this.listinfo(this.data.tid)

          }
        }
      } else {
        wx.showToast({
          title: res.data.msg,
        })
        // wx.navigateTo({
        //   url: '../login/login',
        // })
      }


    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: options.title,
    })
    this.setData({
      sellmoney: options.sellmoney,
      shopid: options.id,
      userid: wx.getStorageSync('userinfo').id
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    ddt("get", "/api/products/shopinfo?shopid=" + this.data.shopid).then(res => {
      wx.setNavigationBarTitle({
        title: res.data.data.shopname,
      })
    })
    this.typelist()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})